package com.nest4cloud.nest.account.controller;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nest4cloud.nest.account.entity.AccountTypeEntity;
import com.nest4cloud.nest.account.service.AccountTypeService;
import com.nest4cloud.nest.common.core.util.R;
import com.nest4cloud.nest.common.log.annotation.SysLog;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import com.nest4cloud.nest.common.security.annotation.HasPermission;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 账户类型表
 *
 * @author nest
 * @date 2024-11-25 09:29:03
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/accAccountType" )
@Tag(description = "accAccountType" , name = "账户类型表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AccountTypeController {

    private final AccountTypeService accountTypeService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param accountType 账户类型表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @HasPermission("account_accAccountType_view")
    public R getAccountTypePage(@ParameterObject Page page, @ParameterObject AccountTypeEntity accountType) {
        LambdaQueryWrapper<AccountTypeEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StrUtil.isNotBlank(accountType.getName()),AccountTypeEntity::getName,accountType.getName());
		wrapper.eq(ObjUtil.isNotNull(accountType.getProperty()),AccountTypeEntity::getProperty, accountType.getProperty() == null ? null : Integer.valueOf(accountType.getProperty().toString()));
        return R.ok(accountTypeService.page(page, wrapper));
    }


    /**
     * 通过条件查询账户类型表
     * @param accountType 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询" , description = "通过条件查询对象" )
    @GetMapping("/details" )
    @HasPermission("account_accAccountType_view")
    public R getDetails(@ParameterObject AccountTypeEntity accountType) {
        return R.ok(accountTypeService.list(Wrappers.query(accountType)));
    }

	@Operation(summary = "通过用户ID查询账户分类" , description = "通过用户ID查询账户分类" )
	@GetMapping("/getAccountTypeByUserId")
	public R getAccountTypeByUserId(@ParameterObject AccountTypeEntity accountType) {
		return R.ok(accountTypeService.getAccountTypeByUserId());
	}

    /**
     * 新增账户类型表
     * @param accountType 账户类型表
     * @return R
     */
    @Operation(summary = "新增账户类型表" , description = "新增账户类型表" )
    @SysLog("新增账户类型表" )
    @PostMapping
    @HasPermission("account_accAccountType_add")
    public R save(@RequestBody AccountTypeEntity accountType) {
        return R.ok(accountTypeService.saveEntity(accountType));
    }

    /**
     * 修改账户类型表
     * @param accountType 账户类型表
     * @return R
     */
    @Operation(summary = "修改账户类型表" , description = "修改账户类型表" )
    @SysLog("修改账户类型表" )
    @PutMapping
    @HasPermission("account_accAccountType_edit")
    public R updateById(@RequestBody AccountTypeEntity accountType) {
        return R.ok(accountTypeService.updateById(accountType));
    }

    /**
     * 通过id删除账户类型表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除账户类型表" , description = "通过id删除账户类型表" )
    @SysLog("通过id删除账户类型表" )
    @DeleteMapping
    @HasPermission("account_accAccountType_del")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(accountTypeService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param accountType 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @HasPermission("account_accAccountType_export")
    public List<AccountTypeEntity> exportExcel(AccountTypeEntity accountType,Long[] ids) {
        return accountTypeService.list(Wrappers.lambdaQuery(accountType).in(ArrayUtil.isNotEmpty(ids), AccountTypeEntity::getId, ids));
    }

    /**
     * 导入excel 表
     * @param accountTypeList 对象实体列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @HasPermission("account_accAccountType_export")
    public R importExcel(@RequestExcel List<AccountTypeEntity> accountTypeList, BindingResult bindingResult) {
        return R.ok(accountTypeService.saveBatch(accountTypeList));
    }
}
